package com.skivingcloud.admin.sys.controller;


import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysBusinessCommon;
import com.skivingcloud.admin.sys.service.ISysBusinessCommonService;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;


/**
 * <p>
 * 通用业务数据表(业务的表单数据） 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/sys/businessCommon")
public class SysBusinessCommonController {
	private static Logger log = LoggerFactory.getLogger(SysBusinessCommonController.class);

	@Autowired
	private ISysBusinessCommonService sysBusinessCommonService;

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = sysBusinessCommonService.queryPage(params);
		return R.ok().put("page", pages);
	}

	/**
	 * 新增
	 * 
	 * @param sysBusinessCommon
	 * @return
	 */
	@UserOperateLog(module = "通用业务", methods = "save", description = "业务数据新增")
	@PostMapping("/save")
	public R save(@RequestBody SysBusinessCommon sysBusinessCommon) {
		try {
			sysBusinessCommonService.add(sysBusinessCommon, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存业务数据失败!", e);
			return R.error("保存业务数据失败!");
		}
	}

	/**
	 * 修改
	 * 
	 * @param sysBusinessCommon
	 * @return
	 */
	@UserOperateLog(module = "通用业务", methods = "update", description = "修改业务数据")
	@PostMapping("/update")
	public R update(@RequestBody SysBusinessCommon sysBusinessCommon) {
		try {
			sysBusinessCommonService.update(sysBusinessCommon, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存业务数据失败!", e);
			return R.error("保存业务数据失败!");
		}
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@UserOperateLog(module = "通用业务", methods = "delete", description = "删除业务数据")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			sysBusinessCommonService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除业务数据失败!", e);
			return R.error("删除业务数据失败!");
		}
	}

	/**
	 * 根据主键查询
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		SysBusinessCommon sysBusinessCommon = sysBusinessCommonService.selectById(id);
		return R.ok().put("data", sysBusinessCommon);
	}

	/**
	 * 通用的业务提交方法，以json格式存储表单数据，如果定义了工作流则会启动一个流程
	 * @param jsonNode
	 * @return
	 */
	@UserOperateLog(module = "通用业务", methods = "submitBusinessCommonAdd", description = "业务数据新增")
	@PostMapping("/submitBusinessCommonAdd")
	public R submitBusinessCommonAdd(@RequestBody JsonNode jsonNode) {
		try {
			sysBusinessCommonService.submitBusinessCommonAdd(jsonNode, UserUtil.getUser());
			return R.ok();
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			return R.error(e.getMessage());
		}catch (Exception e) {
			log.error("保存业务数据失败!", e);
			return R.error("保存业务数据失败!");
		}
	}
	/**
	 * 通用的业务提交方法，以json格式存储表单数据，如果定义了工作流则会提交流程流转
	 * @param jsonNode
	 * @return
	 */
	@UserOperateLog(module = "通用业务", methods = "submitBusinessCommonUpdate", description = "业务数据修改")
	@PostMapping("/submitBusinessCommonUpdate")
	public R submitBusinessCommonUpdate(@RequestBody JsonNode jsonNode) {
		try {
			sysBusinessCommonService.submitBusinessCommonUpdate(jsonNode, UserUtil.getUser());
			return R.ok();
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			return R.error(e.getMessage());
		}catch (Exception e) {
			log.error("保存业务数据失败!", e);
			return R.error("保存业务数据失败!");
		}
	}
	
}
